package mmc.server.shell.ops.function;

import java.lang.reflect.Method;

import mmc.server.shell.OperationNotAcceptedException;
import mmc.server.shell.Shell;
import mmc.server.shell.ShellException;
import mmc.server.shell.Util;
import mmc.server.shell.ops.Function;
import mmc.server.shell.ops.constant.StringValue;

public class Printf extends Function {

    public Printf(String text) throws ShellException {
        super(text, "printf", 1, true);
        
    	if(!(arguments.getArgument(0) instanceof StringValue)){
    		throw new OperationNotAcceptedException(true, "printf's first argument must be a string! (was a \""+arguments.getArgument(0).getClass().getName()+"\")");
    	}
    }

    @Override
    public double execute() {
    	throw new RuntimeException(this+" has no double output!");
    }
    
    @Override
    public Object executeComplex(){
    	Method m = Util.searchMethod(Shell.out.getClass(), "printf", arguments, false);
    	Util.callComplexMethod(m, Shell.out, arguments);
    	return null;
    }
    
    @Override
    public Class<?> getOutputType(){
    	return void.class;
    }
    
    public static String getDescription(){
    	return formatHelpString("printf(str,args)","printf");
    }
}
